<?php

class present_tutor extends Controller {

	function present_tutor()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->model('p_tutormodel');
		$this->load->library('pagination');
		
		$per_page = 6;
	    $total = $this->p_tutormodel->count_all();
   		$data['tutor'] = $this->p_tutormodel->getAllLimit($per_page, $this->uri->segment(3));
	    
	    
	    $base_url = site_url('present_tutor/index/');
	    $config['base_url'] = $base_url;
	    $config['total_rows'] = $total;
	    $config['per_page'] = $per_page;
	    $config['uri_segment'] = '3';
	
	    $this->pagination->initialize($config); 
		
		$data['paging'] = str_replace('index.html/', 'index/', $this->pagination->create_links());
		
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		
		$this->load->view('f_header', $data);
		$this->load->view('f_present_tutor', $data);
		$this->load->view('f_footer', $data);
	}
	
	function sorts()
	{
		
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('p_tutormodel');
	    $data['data'] = $this->p_tutormodel->getAllTutor();

		$this->load->view('header');
		$this->load->view('tutor-present-list-form', $data);
		$this->load->view('footer');
	}
	
	function add()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $data = array('p_tutor_id' => null, 'name' => null, 'detail' => null);
	    
		$this->load->view('header');
		$this->load->view('tutor-present-detail-form', $data);
		$this->load->view('footer');
	}
	
	function saveSuccess()
	{
	    $this->load->model('p_tutormodel');
	    $data['data'] = $this->p_tutormodel->getTutor();
		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
		$this->load->view('header');
		$this->load->view('tutor-present-list-form', $data);
		$this->load->view('footer', $msg);
	}
	function detail()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('p_tutormodel');
	    $stuid = $this->input->post('stuid'); 
	    if($stuid != null) {
			$data = $this->p_tutormodel->getByID($this->input->post('stuid'));		
			$this->load->view('header');
			$this->load->view('tutor-present-detail-form', $data);
			$this->load->view('footer');	    	
	    } else {
	    	// move seq
	    	$idArr = $this->input->post('sid');
	    	$seqArr = $this->input->post('txtSeq');
	    	for($i = 0; $i < count($idArr); $i++) {
	    		$this->p_tutormodel->updateSeq($idArr[$i], $seqArr[$i], $this->session->userdata('username'));
	    	}
	    	$this->sorts();
	    }	    
	}
	
	function save()
	{
		$this->load->model('p_tutormodel');
		$seq = $this->p_tutormodel->getMaxSeq();
		
		$data = array(
               'name' => $this->input->post('stuName') ,
               'detail' => $this->input->post('stuDetail') ,
			   'show' => $this->input->post('showMainPage'),
			   'seq' => $seq,
			   
            );
        if($this->input->post('stuid') != null && $this->input->post('stuid') != '') {
        	$data['p_tutor_id'] = $this->input->post('stuid');
        	$data['update_by'] = $this->session->userdata('username');
        	$key = $this->input->post('stuid');
        	$this->p_tutormodel->update($data);
        } else {
        	$data['create_by'] = $this->session->userdata('username');
			$key = $this->p_tutormodel->add($data);
        }
        if(isset($_FILES) && @$_FILES['userfile']['error'] == '0') {
			$config['upload_path'] = './data-image/p_tutor/';
			$config['allowed_types'] = 'gif|jpg|png|';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $key;
			$this->load->library('upload', $config);
			$msg = array();
			if ( ! $this->upload->do_upload())
			{
				//$error = array('error' => $this->upload->display_errors());
				$msg['msg'] = "ไม่สามารถ upload ไฟล์ได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'p_tutor_id' => $key, 
	               'img_path' => $data['upload_data']['file_name']   
	            );
				$this->p_tutormodel->updateImagePath($Imgdata);
				$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
			}
        } else {
        	$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        }
		
		$data = $this->p_tutormodel->getByID($key);		
		$this->load->view('header');
		$this->load->view('tutor-present-detail-form', $data);
		$this->load->view('footer', $msg);
	}
	
	function delete()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('p_tutormodel');
	    $id = $this->input->post('stuid'); 
	    if($id != null) {
			$this->p_tutormodel->delete($id);				
	    }   
	  $this->add();
	}
}

?>